package com.cgm.controller;

import com.cgm.controller.utils.ExcelExportUtil;
import com.cgm.service.dto.BookDTO;
import com.cgm.service.dto.StudentDTO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/5/3.
 */
@Controller
public class ExcelOutController {

    @RequestMapping("check/export.do")
    public String checkOut(HttpServletRequest request){
        List<StudentDTO> list = new ArrayList<StudentDTO>();
        ExcelExportUtil<StudentDTO> ex = new ExcelExportUtil<StudentDTO>();
        String[] headers = { "学号", "姓名", "年龄", "性别", "出生日期" };
        List<StudentDTO> dataset = new ArrayList<StudentDTO>();
        dataset.add(new StudentDTO(10000001, "张三", 20, true, new Date()));
        dataset.add(new StudentDTO(20000002, "李四", 24, false, new Date()));
        dataset.add(new StudentDTO(30000003, "王五", 22, true, new Date()));
        // 测试图书
        ExcelExportUtil<BookDTO> ex2 = new ExcelExportUtil<BookDTO>();
        String[] headers2 = { "图书编号", "图书名称", "图书作者", "图书价格", "图书ISBN", "图书出版社",
                "封面图片" };
        List<BookDTO> dataset2 = new ArrayList<BookDTO>();
        try {
            BufferedInputStream bis = new BufferedInputStream(
                    new FileInputStream("C:\\Users\\Administrator\\Pictures\\img20120822145157621090.jpg"));
            byte[] buf = new byte[bis.available()];
            while ((bis.read(buf)) != -1) {
                //
            }
            dataset2.add(new BookDTO(1, "jsp", "leno", 300.33f, "1234567",
                    "清华出版社", buf));
            dataset2.add(new BookDTO(2, "java编程思想", "brucl", 300.33f, "1234567",
                    "阳光出版社", buf));
            dataset2.add(new BookDTO(3, "DOM艺术", "lenotang", 300.33f, "1234567",
                    "清华出版社", buf));
            dataset2.add(new BookDTO(4, "c++经典", "leno", 400.33f, "1234567",
                    "清华出版社", buf));
            dataset2.add(new BookDTO(5, "c#入门", "leno", 300.33f, "1234567",
                    "汤春秀出版社", buf));

            OutputStream out = new FileOutputStream("E://a.xls");
            OutputStream out2 = new FileOutputStream("E://b.xls");
            ex.exportExcel(headers, dataset, out);
            ex2.exportExcel(headers2, dataset2, out2);
            out.close();
            out2.close();
            System.out.println("excel导出成功！");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "success" ;
    }
}
